/**
 * 
 */
package io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

import game.UnitType;

/**
 * @author Remi REBILLARD
 *
 */
public class ImportUnitType {
	private BufferedReader buffer;
	
	private String header;
	private String[] content;
	
	/**
	 * @param inputstream
	 */
	public ImportUnitType( InputStream is ){
		this.buffer = new BufferedReader( new InputStreamReader( is ) );
	}
	
	/**
	 * @return the data of file for UnitType
	 */
	public HashMap< String, UnitType > read() throws IOException{
		
		HashMap< String, UnitType > data = new HashMap< String, UnitType >();
		header = buffer.readLine();
		
		if( !header.equals( "game_db_units\t1" ) )
			throw new IOException( "Invalid Header of UnitType" );
		
		while( buffer.ready() ){
			
			content = buffer.readLine().split( "\t" );
			UnitType tmpUnit = new UnitType
			(
				content[0]	//NAME		
			);
			
			tmpUnit.setImages( 0, content[1] );							//GREY
			tmpUnit.setImages( 1, content[2] );							//RED
			tmpUnit.setImages( 2, content[3] );							//BLUE
			tmpUnit.setImages( 3, content[4] );							//GREEN
			tmpUnit.setImages( 4, content[5] );							//YELLOW
			tmpUnit.setMovementRange( Integer.parseInt( content[10] ) );//MOVERANGE
			tmpUnit.setMovementType( content[11] );						//MOVETYPE
			
			tmpUnit.setPrimaryWeaponType(
				new game.WeaponType	(
					content[12],						//NAME
					Integer.parseInt( content[14] ),	//MINRANGE
					Integer.parseInt( content[15] ),	//MAXRANGE
					Integer.parseInt( content[16] )		//AMMO
				)
			);
			tmpUnit.setCanCapture( Boolean.parseBoolean(content[23]) );	//CANCAPTURE
			
			data.put( content[0], tmpUnit );
		}
		return data;
	}
}